home *** CD-ROM | disk | FTP | other *** search
- /********************************************************************************************
-
- file: ControlStripSample.c
-
- contains: a sample Control Strip module
-
- author: SC [8/29/93]
- modified: DTS 06/07/94 rename files, comments to reflect name is
- Control Strip (as opposed to Status Bar)
-
- Copyright © 1993 by Apple Computer, Inc. All rights reserved.
-
- ********************************************************************************************/
-
- #ifndef SystemSevenOrLater
- #define SystemSevenOrLater 1
- #endif
-
- #include <Memory.h>
- #include <Menus.h>
- #include <Quickdraw.h>
- #include <Resources.h>
- #include <ToolUtils.h>
- #include <Types.h>
- #include <Icons.h>
- #include "ControlStrip.h"
- #include "ControlStripSample.h"
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // global variables
-
- typedef struct Globals { // global variables we use while we exist
- Handle lastIcon; // last icon to be displayed
- Handle firstIcon; // icon suites displayed in the Control Strip module
- Handle secondIcon;
- Handle thirdIcon;
- PicHandle popupArrowPicture; // picture to show we have a popup menu
- Handle helpStrings; // balloon help strings for each state
- short helpStringIndex; // which help string to display
- short whichIcon; // which icon we're drawing
- MenuHandle configMenu; // menu to select display options
- } Globals;
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // information saved across restarts
-
- typedef struct SavedSettings {
- OSType signature; // signature to verify it's for this module
- short whichIcon; // which icon we're drawing
- } SavedSettings;
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // prototypes
-
- long Initialize();
- void CleanUp(Globals **globs);
- void DrawCurrentIcon(Globals *gb, Rect *statusRect);
- void GetCurrentIcon(Globals *gb);
- long HandleMouseClick(Globals *gb, Rect *statusRect);
- short SavePreferences(Globals *gb);
-
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // entry point
-
- pascal long main(unsigned long message, Globals **globs, Rect *statusRect, GrafPtr statusPort) {
- #pragma unused(statusPort)
- char savedState;
- Globals *gb;
- long result;
- Handle theLastIcon;
- Str255 helpString;
-
-
- if ((long)globs > 0) { // if we have globals allocated,
- savedState = HGetState((Handle)globs); // save the locked/unlocked state,
- HLock((Handle)globs); // lock the handle to the globals,
- gb = *globs; // and point to the globals directly
- }
-
- result = 0; // just return zero for unknown messages
- switch(message) {
- case sdevInitModule: // initialize the module
- if ((result = Initialize()) > 0) break; // and exit if successful
- globs = (Globals **)result;
-
- case sdevCloseModule: // clean up before being closed
- CleanUp(globs);
- globs = 0L;
- break;
-
- case sdevFeatures: // return feature bits
- result = (1<<sdevWantMouseClicks) |\
- (1<<sdevDontAutoTrack) |\
- (1<<sdevHasCustomHelp);
- break;
-
- case sdevGetDisplayWidth: // return display width
- result = IconWidth +
- (**gb->popupArrowPicture).picFrame.right - (**gb->popupArrowPicture).picFrame.left;
- break;
-
- case sdevPeriodicTickle: // periodic tickle when nothing else is happening
- theLastIcon = gb->lastIcon; // save the last state
- GetCurrentIcon(gb); // update everything
- if (theLastIcon == gb->lastIcon) break; // if everything's the same, just exit
- result += 1<<sdevHelpStateChange; // flag a state change in case we're displaying help
-
- statusRect->right = statusRect->left + IconWidth;
- EraseRect(statusRect); // erase the icon
-
- case sdevDrawStatus: // update the interface in the Control Strip
- DrawCurrentIcon(gb, statusRect);
- break;
-
- case sdevMouseClick: // user clicked on the module's display area in the Control Strip
- result = HandleMouseClick(gb, statusRect);
- break;
-
- case sdevSaveSettings: // save changed settings
- result = SavePreferences(gb);
- break;
-
- case sdevShowBalloonHelp: // display custom balloon help
- SBGetDetachedIndString(helpString, gb->helpStrings, gb->helpStringIndex);
- SBShowHelpString(statusRect, helpString);
- break;
- }
-
- if ((long)globs > 0) // if we have globals allocated,
- HSetState((Handle)globs, savedState); // restore the locked/unlocked state
-
- return(result);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // initializes the module
-
- long Initialize() {
- short i;
- long result;
- Globals **globs, *gb;
- Str255 prefsResourceName;
- SavedSettings **preferences;
- Handle *theIconSuite;
-
- result = -1; // assume failure
-
- if (! (globs = (Globals **)NewHandleClear(sizeof(Globals))))
- return(MemError()); // allocate the globals
-
- HLock((Handle)globs); // lock the globals while using them
- gb = *globs; // and get a pointer to them
-
- // load and detach the icon suites
-
- theIconSuite = &gb->firstIcon;
- for (i=ThirdIconID-FirstIconID; i>=0; i--) {
- if (result = SBGetDetachIconSuite(theIconSuite, ThirdIconID-i, svAllSmallData))
- goto done;
- theIconSuite++;
- }
-
- // load and detach the ‘up arrow’ picture
-
- if (! (gb->popupArrowPicture = GetPicture(PopupArrowPictID))) goto done;
- DetachResource((Handle)gb->popupArrowPicture);
-
- // load and detach the configuration menu
-
- if (! (gb->configMenu = GetMenu(ConfigMenuID))) goto done;
- DetachResource((Handle)gb->configMenu);
-
- // load and detach the help strings
-
- if (! (gb->helpStrings = Get1Resource('STR#', HelpStringsID))) goto done;
- DetachResource(gb->helpStrings);
-
- // get the module's saved preferences, if any, and configure the module
-
- gb->whichIcon = mShowFirstIcon;
- SBGetDetachedIndString(prefsResourceName, gb->helpStrings, sPrefResourceName);
- if (! SBLoadPreferences(prefsResourceName, (Handle *)&preferences) &&
- ((**preferences).signature == 'Samp'))
- gb->whichIcon = (**preferences).whichIcon;
-
- GetCurrentIcon(gb); // initialize which icon to draw
-
- HUnlock((Handle)globs); // unlock the globals
-
- result = (long)globs; // return the handle to the globals as the result
-
- done:
- return(result); // return either a handle or an error code
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // disposes of our storage before we get closed
-
- void CleanUp(Globals **globs) {
- Globals *gb;
-
- if ((long)globs <= 0) return;
-
- HLock((Handle)globs);
- gb = *globs;
-
- if (gb->firstIcon) DisposeIconSuite(gb->firstIcon, true);
- if (gb->secondIcon) DisposeIconSuite(gb->secondIcon, true);
- if (gb->thirdIcon) DisposeIconSuite(gb->thirdIcon, true);
-
- if (gb->popupArrowPicture) DisposeHandle((Handle)gb->popupArrowPicture);
-
- if (gb->configMenu) DisposeMenu(gb->configMenu);
-
- if (gb->helpStrings) DisposeHandle(gb->helpStrings);
-
- DisposeHandle((Handle)globs);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // draws the current icon
-
- void DrawCurrentIcon(Globals *gb, Rect *statusRect) {
- short arrowHeight;
-
- // draw the current icon
-
- statusRect->right = statusRect->left + IconWidth;
- (void) PlotIconSuite(statusRect, atNone, ttNone, gb->lastIcon);
-
- // draw an ‘up arrow’ to show that the module has a popup menu
-
- arrowHeight = (**gb->popupArrowPicture).picFrame.bottom - (**gb->popupArrowPicture).picFrame.top;
- statusRect->left = statusRect->right;
- statusRect->right += (**gb->popupArrowPicture).picFrame.right - (**gb->popupArrowPicture).picFrame.left;
- statusRect->top = (statusRect->top + statusRect->bottom - arrowHeight) >> 1;
- statusRect->bottom = statusRect->top + arrowHeight;
- DrawPicture(gb->popupArrowPicture, statusRect);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // stuffs the icon suite handle of the current icon into gb->lastIcon,
- // and the related help STR# index into gb->helpStringIndex
-
- void GetCurrentIcon(Globals *gb) {
-
- switch (gb->whichIcon) {
- case mShowFirstIcon:
- gb->lastIcon = gb->firstIcon;
- gb->helpStringIndex = sFirstIconHelp;
- break;
-
- case mShowSecondIcon:
- gb->lastIcon = gb->secondIcon;
- gb->helpStringIndex = sSecondIconHelp;
- break;
-
- case mShowThirdIcon:
- gb->lastIcon = gb->thirdIcon;
- gb->helpStringIndex = sThirdIconHelp;
- break;
- }
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // handles a mouse click on the module's display by tracking a popup menu to change settings
-
- long HandleMouseClick(Globals *gb, Rect *statusRect) {
- short menuItem;
- long result;
-
- SetItemMark(gb->configMenu, gb->whichIcon, sdevMenuItemMark);
- // check the item for the current icon
-
- menuItem = SBTrackPopupMenu(statusRect, gb->configMenu);
- // call the utility routine to display a popup menu
-
- CheckItem(gb->configMenu, gb->whichIcon, false);// uncheck the item for the previous icon
-
- result = 0;
- if ((menuItem > 0) && (menuItem != gb->whichIcon)) {
- // if something was selected,
- gb->whichIcon = menuItem; // save the menu item number,
- GetCurrentIcon(gb); // update the icon,
- result = 1<<sdevNeedToSave; // and notify the Control Strip that we need to update preferences
- }
-
- return(result);
- }
-
-
- //////////////////////////////////////////////////////////////////////////////////////////////
- //
- // saves the module's settings so they'll be available across restarts
-
- short SavePreferences(Globals *gb) {
- short result;
- SavedSettings **preferences;
- Str255 prefsResourceName;
-
- preferences = (SavedSettings**)NewHandle(sizeof(SavedSettings));
- if (! (result = MemError())) { // allocate a block to hold the settings
-
- (**preferences).signature = 'Samp'; // include a signature to verify it's ours
- (**preferences).whichIcon = gb->whichIcon; // stuff in the menu item number of the current icon
-
- SBGetDetachedIndString(prefsResourceName, gb->helpStrings, sPrefResourceName);
- // get the name of the preferences resource
- result = SBSavePreferences(prefsResourceName, (Handle)preferences);
- // save the settings in the Control Strip's preferences file
-
- DisposeHandle((Handle)preferences); // get rid of the block
- }
-
- return(result);
- }
-